# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 结构化数据-TypedDict.py
# @Author: dongguangwen
# @Date  : 2025-03-23 20:39
from typing import Optional
from typing_extensions import Annotated, TypedDict
from langchain_openai import ChatOpenAI


model = ChatOpenAI(model="Pro-128K", api_key="dzCDkUHufCZEsuWlfVQd:",
                   base_url="https://spark-api-open.xf-yun.com/v1")


# TypedDict
class Joke(TypedDict):
    """Joke to tell user."""

    setup: Annotated[str, ..., "The setup of the joke"]

    # Alternatively, we could have specified setup as:

    # setup: str                    # no default, no description
    # setup: Annotated[str, ...]    # no default, no description
    # setup: Annotated[str, "foo"]  # default, no description

    punchline: Annotated[str, ..., "The punchline of the joke"]
    rating: Annotated[Optional[int], None, "How funny the joke is, from 1 to 10"]


structured_llm = model.with_structured_output(Joke)

structured_llm.invoke("Tell me a joke about cats")
